<?php
	require 'conn.php';
	$username = trim($_POST['username']);
	$password = trim($_POST['password']);
	$repassword = trim($_POST['repassword']);
	$email = trim($_POST['email']);
	$telephone = trim($_POST['telephone']);
	$nickname = trim($_POST['nickname']);
	$type = trim($_POST['type']);
	$createTime = time();
	
	if ($username =="" || $password=="" || $repassword=="" || $email=="" || $telephone=="" || $nickname=="" || $type=="")
	{
		echo "<script language=javascript>alert('带*号的选项必须填写全'); history.go(-1)</script>";
		exit;
	}
	if ($password != $repassword) {
		echo "<script language=javascript>alert('两次密码输入不一致'); history.go(-1)</script>";
		exit;
	}
	
	$sql = "SELECT username FROM user where username='$username'";
	$result = mysql_query($sql);
	
	if ($myrow = mysql_fetch_row($result)) {
		echo("<script language=javascript>alert('用户名已存在！'); history.go(-1)</script>");
		exit;
	}
	
	$sql = "SELECT nickname FROM user where nickname='$nickname'";
	$result = mysql_query($sql);
	
	if ($myrow = mysql_fetch_row($result)) {
		echo("<script language=javascript>alert('该昵称已存在！'); history.go(-1)</script>");
		exit;
	}
	
	$query = "INSERT INTO user(username, password, email, telephone, nickname, type, createtime)"
						."VALUES('$username','$password','$email','$telephone','$nickname','$type','$createTime')";
						
	$result = mysql_query($query) or die("插入出现错误：". mysql_errno());
	$rowid = mysql_insert_id();
	if ($rowid > 0) {
		echo "添加用户成功！<hr><p>";
		echo "你添加的用户为：".$nickname."<p>";
		echo "<br><a href='addUserForm.php'>继续添加</a>";
	}
	else {
		echo "添加新用户失败！<hr><p>";
		echo "<br><a href='addUserForm.php'>重新添加</a>";
	}
			
?>